package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import io.renren.modules.reception.init.TableThemeCache;
import io.renren.modules.store.entity.TStoreDiningTableEntity;
import io.renren.modules.store.service.TStoreDiningTableService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TTableThemeEntity;
import io.renren.modules.store.service.TTableThemeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-08-12 22:48:27
 */
@RestController
@RequestMapping("store/ttabletheme")
public class TTableThemeController extends AbstractController {
    @Autowired
    private TTableThemeService tTableThemeService;
    @Autowired
    private TStoreDiningTableService storeDiningTableService;

    /**
     * 信息
     */
    @RequestMapping("/info")
    public R info(){
        TTableThemeEntity themeEntity = tTableThemeService.queryTheme(getSeller());
        return R.ok().put("tTableTheme", themeEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/themeSet")
    public R save(@RequestBody TTableThemeEntity tTableTheme){
        tTableTheme.setSellerId(getSeller());
        TTableThemeEntity themeEntity = tTableThemeService.getOne(
                new LambdaQueryWrapper<TTableThemeEntity>()
                        .eq(TTableThemeEntity::getSellerId, getSeller())
        );
        if (themeEntity == null){
            tTableThemeService.save(tTableTheme);
            return R.ok();
        }
        tTableThemeService.updateById(tTableTheme);
        //更新缓存
        TableThemeCache.put(tTableTheme.getSellerId(), tTableTheme);

        //更新餐桌状态
        TableThemeChangeFactory(1,tTableTheme.getFreeColor());
        TableThemeChangeFactory(2,tTableTheme.getEatColor());
        TableThemeChangeFactory(3,tTableTheme.getWaitColor());
        TableThemeChangeFactory(5,tTableTheme.getPreColor());
        TableThemeChangeFactory(6,tTableTheme.getStopColor());
        return R.ok();
    }

    public void TableThemeChangeFactory(Integer status,String color){
        storeDiningTableService.update(
                new LambdaUpdateWrapper<TStoreDiningTableEntity>()
                        .eq(TStoreDiningTableEntity::getStatus,status)
                        .eq(TStoreDiningTableEntity::getSellerId,getSeller())
                        .set(TStoreDiningTableEntity::getStyle, color)
        );
    }

}
